var baseUrl = $("script[baseUrl]").attr('baseUrl');

var tableid=$("[name='mytable']").attr('id');
var tablefilter=$("[name='mytable']").attr('lay-filter');
var tableIndex=$("[name='tableIndex']").attr('id');
var edit_del=$("[name='edit_del']").attr('id');


layui.use(['jquery', 'layer', 'table'], function () {
    var layer = layui.layer, table = layui.table;

    //让层自适应iframe
    $("[name='mybutton']").on('click', function () {
        var index = layer.open({
            type: 2,
            content: baseUrl+ '/student/goAdd',
            area: ['600px', '600px'],
            maxmin: true,
            end: function () {
                table.reload(tableid, {});
            }
        });
        parent.layer.iframeAuto(index);
    });

    //表格渲染
    table.render({
        elem:'#'+tableid
        , url: baseUrl+'/student/getStudentPage'
        , method: 'post'
        , page: {layout: ['limit', 'count', 'prev', 'page', 'next', 'skip']}
        , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        , cols: [[
            {field: '', align: 'center', width: 70, title: '序号', toolbar: '#'+tableIndex}
            , {field: 'stuName', title: '学生姓名'}
            , {field: 'stuAge', title: '学生年龄'}
            , {field: 'stuGrade', title: '学生成绩'}
            , {field: 'stuNumber', title: '学生学号'}
            , {field: 'stuCreateTime', title: '创建日期'}
            , {field: 'right', align: 'center', width: 150, toolbar: '#'+edit_del, title: '操作'}
        ]]
    });

    //监听修改按钮
    table.on('tool('+tablefilter+')', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            // 编辑
            var index = layer.open({
                type: 2,
                content:baseUrl+ '/student/goUpdate?id=' + data.id,
                area: ['800px', '500px'],
                maxmin: true,
                end: function () {
                    table.reload(tableid, {});
                }
            });
            parent.layer.iframeAuto(index);

        } else if (obj.event === 'del') {
            layer.confirm('真的要删除么？', function (index) {
                // 写删除方法
                $.post(baseUrl+"/student/del", {"id": data.id}, function (data) {
                    if (data.status == 200) {
                        layer.msg(data.message, {icon: 1, time: 1000});
                        // 前端修改
                        layer.close(index);
                        table.reload(tableid, {});
                    } else {
                        layer.msg(data.message, {icon: 0, time: 1000});
                        layer.close(index);
                    }
                });
            });
        }
    });
});

//监听搜索按钮点击事件
function search() {
    var table = layui.table;
    var searchText =$("[name='search']").attr('id');
    var search=$('#'+searchText).val();
    console.log(search);
    table.reload(tableid, {
        where: { //设定异步数据接口的额外参数，任意设
            search: search
        }
        , page: {
            curr: 1 //重新从第 1 页开始
        }
    });
}


